// pages/punch/punch.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		signature: getApp().globalData.signature,
		token: getApp().globalData.token,
		nickName: getApp().globalData.nickName,
		desc: getApp().globalData.desc,
		openId: getApp().globalData.openId,
		avatarUrl: getApp().globalData.avatarUrl,
		punchCards: [],
		cursor: 0,
		isQuery: false,
		isNoMore: false,
		showDetails: false,
		selectPunchCard: null,
	},

	onClose() {
		this.setData({
			showDetails: false,
		})
	},


	//跳转到详情
	jumpToDetails(e) {
		this.setData({
			selectPunchCard: null,
			followerCursor: 0,
			msgCursor: 0,
			users: [],
			msgs: [],
		})
		const index = e.currentTarget.dataset.index
		this.setData({
			showDetails: true,
			selectPunchCard: this.data.punchCards[index],
		})

	},

	/**
	 * 获取活动列表
	 * @param {用户请求token} token 
	 */
	queryPunchCards(token) {
		if (this.data.isQuery) return;
		this.setData({
			isQuery: true,
		})
		const dayjs = require('dayjs')
		console.log("查询列表 token = ", token)
		console.log("查询列表 cursor = ", this.data.cursor)
		wx.request({
			url: 'https://marklife.love/5h4h/punch/get',
			method: 'POST',
			//传参使用form-data格式
			header: {
				'content-type': 'application/x-www-form-urlencoded' //修改此处即可
			},
			data: {
				token: token,
				type: 0,
				limit: 10,
				cursor: this.data.cursor,
			},
			complete: () => {
				this.setData({
					isQuery: false
				})
			},
			success: (res) => {
				var errCode = res.data.err
				console.log("查询", res.data)
				if (errCode == 1000) {
					//查询活动列表成功
					console.log("查询成功")
					getApp().globalData.needReRefresh = false
					if (res.data.data.length == 0) {
						this.setData({
							isNoMore: true,
						})
					}

					if (this.data.cursor == 0) {
						this.setData({
							punchCards: res.data.data,
							cursor: res.data.cursor
						})
					} else {
						var temp = [...this.data.punchCards, ...res.data.data]
						this.setData({
							punchCards: [...this.data.punchCards, ...res.data.data],
							cursor: res.data.cursor
						})
					}

					wx.stopPullDownRefresh()
					wx.hideLoading()
				} else {
					wx.stopPullDownRefresh()
					wx.hideLoading()
					//查询活动列表失败 未知原因
					wx.showModal({
						title: '查询失败',
						content: '抱歉，查询失败，我也没时间去排查错误原因',
						showCancel: false,
						complete: (res) => {
							if (res.cancel) {}
							if (res.confirm) {}
						}
					})
				}
			},
			fail: (res) => {
				wx.stopPullDownRefresh()
				wx.hideLoading()
				wx.showModal({
					title: '查询失败',
					content: '抱歉，查询失败，我也没时间去排查错误原因',
					showCancel: false,
					complete: (res) => {
						if (res.cancel) {}
						if (res.confirm) {}
					}
				})
			}
		})
	},


	/**
	 * 参与打卡
	 */
	addPunch() {
		console.log("参与打卡 token = ", this.data.token)
		wx.request({
			url: 'https://marklife.love/5h4h/punch/attend',
			method: 'POST',
			//传参使用form-data格式
			header: {
				'content-type': 'application/x-www-form-urlencoded' //修改此处即可
			},
			data: {
				token: this.data.token,
				punch_card: this.data.selectPunchCard.key,
			},
			success: (res) => {
				console.log("add punch: ", res.data)
				var errCode = res.data.err
				if (errCode == 1000) {
					wx.hideLoading()
					this.getSimplePunchCard()
					this.setData({
						cursor:0,
					})
					this.queryPunchCards(this.data.token)
					//新增成功
					//新增成功提示
					wx.showModal({
						title: '恭喜您',
						icon: "success",
						content: '打卡成功',
						showCancel: false,
						complete: (res) => {
							if (res.confirm) {
								this.subscribeMessage()
								this.setData({
									cursor: 0,
								})
							}
						}
					})
				} else {
					//新增失败 未知原因
					wx.hideLoading()
					wx.showModal({
						title: '打卡失败',
						content: '抱歉，打卡失败，我也没时间去排查错误原因：' + errCode,
						showCancel: false,
						complete: (res) => {
							if (res.cancel) {}
							if (res.confirm) {}
						}
					})
				}
			}
		})
	},

	//申请授权
	subscribeMessage() {
		wx.requestSubscribeMessage({
			tmplIds: ['yxrr1WvLBJdHD1un8jsGC8sdiEiWG9vyHa1jRqhlMsI'], //这里填入我们生成的模板id
			success(res) {
				console.log('授权成功', res)
			},
			fail(res) {
				console.log('授权失败', res)
			}
		})
	},

	/**
	 * 获取打卡单个
	 */
	getSimplePunchCard() {
		wx.request({
			url: 'https://marklife.love/5h4h/punch/get?key=' + this.data.selectPunchCard.key,
			method: 'POST',
			//传参使用form-data格式
			header: {
				'content-type': 'application/x-www-form-urlencoded' //修改此处即可
			},
			success: (res) => {
				console.log("get simple punch: ", res.data)
				var errCode = res.data.err
				if (errCode == 1000) {
					this.setData({
						selectPunchCard: res.data.data,
					})
				} else {

				}
			}
		})
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		console.log("onShow 1")
		this.setData({
			signature: getApp().globalData.signature,
			token: getApp().globalData.token,
			nickName: getApp().globalData.nickName,
			desc: getApp().globalData.desc,
			openId: getApp().globalData.openId,
			avatarUrl: getApp().globalData.avatarUrl,
		})
		console.log("onShow 2")
		if (this.data.signature == null) {
			getApp().showNoLoginDialog()
			return
		}

		console.log("onShow 4")
		if (getApp().globalData.needReRefreshPuchCards) {
			wx.startPullDownRefresh()
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {
		this.setData({
			showDetails: false,
		})
	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {
		//如果是下拉刷新，那么就要把指针回到0
		this.setData({
			cursor: 0,
			isNoMore: false,
		})
		if (this.data.token != null) {
			this.queryPunchCards(this.data.token)
		}
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {
		let activity = this.data.selectActivty;
		var randomImg = "/imgs/share/share_bboy1.png";
		return {
			title: activity.name,
			desc: activity.desc,
			path: 'pages/todo/todo?key=' + activity.key,
			imageUrl: randomImg, // 可以更换分享的图片
			success: function (res) {
				// 转发成功
				wx.showToast({
					title: '分享成功',
					icon: "none"
				});
			},
			fail: function (res) {
				// 转发失败
				wx.showToast({
					title: '分享失败',
					icon: "none"
				})
			}
		}
	}
})